library(ggplot2)         #for data visualisation 
library(marginaleffects) #for plotting marginal effects
library(mclogit)         #for conditional logit models 
library(patchwork)       #for combining plots
library(ragg)            #for saving data viz in png
library(showtext)        #for displaying fonts in data viz
library(stargazer)       #for model tables in latex
library(survival)        #alternative package for conditional logistic regressions
library(tidyverse)       #for data-wrangling

##data-wrangling----
BrSenSwitch <- read.csv("BrSenSwitch.csv", sep = ";") %>%
               mutate(fusao = ifelse(is.na(fusao), 0, fusao)) %>%
               mutate(fissao = ifelse(is.na(fissao), 0, fissao)) %>%
               mutate(partido_novo = ifelse(is.na(partido_novo), 0, partido_novo)) %>%
               mutate(home_inverted = 1-home) %>%
               filter(partido_novo != 1,
                      fissao != 1) %>%
              mutate(to_be_elected = case_when(legislatura == 48 ~ 1,
                                               legislatura == 49 ~ 2,
                                               legislatura == 50 ~ 1,
                                               legislatura == 51 ~ 2,
                                               legislatura == 52 ~ 1,
                                               legislatura == 53 ~ 2,
                                               legislatura == 54 ~ 1,
                                               legislatura == 55 ~ 2,
                                               legislatura == 56 ~ 1))

double_renovation <- BrSenSwitch %>% filter(to_be_elected == 2) 
single_renovation <- BrSenSwitch %>% filter(to_be_elected == 1)

##models without interactions----
model_a <- mclogit(
  y|choice ~ office + dif1 + party_str + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = BrSenSwitch)

model_b <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = BrSenSwitch)

model_c <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str*elec_comp + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = BrSenSwitch)

model_d <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = single_renovation)

model_e <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + party_str*elec_comp +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = single_renovation)

model_f <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = double_renovation)

model_g <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str*elec_comp + 
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = double_renovation)

##models with clogit----
model1 = clogit(y ~ office + dif1 + party_str + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=BrSenSwitch)

model2 = clogit(y ~ alt_swps + dif1 + party_str + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=BrSenSwitch)

model3 = clogit(y ~ alt_swps + dif1 + party_str*elec_comp + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=BrSenSwitch)

model4 = clogit(y ~ alt_swps + dif1 + party_str + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=single_renovation)


model5 = clogit(y ~ alt_swps + dif1 + party_str + party_str*elec_comp +
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=single_renovation)


model6 = clogit(y ~ alt_swps + dif1 + party_str + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=double_renovation)

model7 = clogit(y ~ alt_swps + dif1 + party_str*elec_comp + 
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=double_renovation)

##stargazer----
stargazer(model1, model2, model3, model4, model5, model6, model7, type = "text")

stargazer(model_a, model_b, model_c, model_d, model_e, model_f, model_g, 
          column.labels   = c("Full Dataset", "M = 1", "M = 2"),
          column.separate = c(3, 2, 2),
          no.space=T, order = c(1,2,3,4,7,5,6,8,9, 10), digits = 2, 
          covariate.labels = c("Preisdential Coalition", "Weighted Participation in Coalition", 
                               "Ideological Distance", "Party Strength", "Party Strength * Electoral Competition",
                               "Home Party", "Governor's Coalition", "Surrogate", "Number of Legislatures",
                               "Rural Population"),
          ##input from clogit models for more detailed infmation
          add.lines=list(c("N","246,994","209,727","209,727", "110,246", "110,246", "99,481", "99,481"),
                         c("R$^{2}", "0.334", "0.342", "0.342", "0.340", "0.340", "0.344", "0.344"),
                         c("Max. Possible R2", "0.339", "0.346", "0.346",  "0.345", "0.345", "0.347", "0.347"),
                         c("Log Likelihood", "-886.359", "-644.211",  "-643.868", "-398.351", "-398.245", "-235.271", "-235.130")))
##models with interactions----
model_h <- mclogit(
y|choice ~ alt_swps + dif1 + party_str + 
  alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
data = BrSenSwitch)

model_i <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = BrSenSwitch)

model_j <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = single_renovation)

model_k <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = single_renovation)

model_l <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = double_renovation)

model_m <- mclogit(
  y|choice ~ alt_swps + dif1 + party_str + 
    alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
    home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted,
  data = double_renovation)

##models with clogit II----
model8 = clogit(y ~  alt_swps + dif1 + party_str + 
                  alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=BrSenSwitch)

model9 = clogit(y ~  alt_swps + dif1 + party_str + 
                  alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
                  home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                  strata(choice), data=BrSenSwitch)

model10 = clogit(y ~  alt_swps + dif1 + party_str + 
                   alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
                   home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                   strata(choice), data=single_renovation)

model11 = clogit(y ~  alt_swps + dif1 + party_str + 
                   alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
                   home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                   strata(choice), data=single_renovation)

model12 = clogit(y ~  alt_swps + dif1 + party_str + 
                   alt_swps*dif1 + alt_swps*party_str + dif1*party_str +
                   home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                   strata(choice), data=double_renovation)

model13 = clogit(y ~  alt_swps + dif1 + party_str + 
                   alt_swps*dif1 + alt_swps*party_str + dif1*party_str + alt_swps*dif1*party_str +
                   home + coligacao_governador + suplente*home_inverted + legislaturas_participadas*home_inverted + pop_rural*home_inverted +
                   strata(choice), data=double_renovation)
##stargazer II----
stargazer(model8, model9, model10, model11, model12, model13, type = "text")

stargazer(model_h, model_i, model_j, model_k, model_l, model_m,
          column.labels   = c("Full Dataset", "M = 1", "M = 2"),
          column.separate = c(2, 2, 2),
          no.space=T, order = c(1,2,3,6,7,8,12,4,5,9,10,11), digits = 2,
          covariate.labels = c("Weighted Participation in Coalition", "Ideological Distance", "Party Strength",
                               "Weighted Part. * Ideological Dist.", "Weighted Part. * Party Str.", 
                               "Ideological Dist. * Party Str.", "Weighted Part. * Ideological Dist. * Party Str.",
                               "Home Party", "Governor's Coalition", "Surrogate", "Number of Legislatures",
                               "Rural Population"),
          add.lines=list(c("N","209,727","209,727", "110,246", "110,246", "99,481", "99,481"),
                         c("R$^{2}", "0.342", "0.342", "0.340", "0.340", "0.344", "0.344"),
                         c("Max. Possible R2", "0.346", "0.346", "0.345", "0.345", "0.347", "0.347"),
                         c("Log Likelihood", "-643.111", "-642.814", "-397.888", "-397.469", "-232.435", "-232.430")))
##fonts----
#These fonts can be obtained here: https://fonts.google.com/specimen/Barlow+Semi+Condensed
font_add(family = "regular", "Barlow Semi Condensed-Regular.ttf")
font_add(family = "bold", "BarlowSemiCondensed-SemiBold.ttf")
showtext_auto()

##data-vis-III- M.E between P.S. vs W.P.C-----
drown <- plot_cme(model_h, variables = "party_str", condition = "alt_swps", type = "link", draw = F)
drown1 <- plot_cme(model_h, variables = "party_str", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = drown, ggplot2::aes(x = alt_swps, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch)+
  ggplot2::geom_ribbon(data = drown, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "Full Dataset") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Party Strength on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"))  


place <- plot_cme(model_j, variables = "party_str", condition = "alt_swps", type = "link", draw = F)
drown3 <- plot_cme(model_j, variables = "party_str", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = place, ggplot2::aes(x = alt_swps, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch)+
  ggplot2::geom_ribbon(data = place, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "M = 1") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Party Strength on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"))  

t3 <- plot_cme(model_l, variables = "party_str", condition = "alt_swps", type = "link", draw = F)
t3.1 <- plot_cme(model_l, variables = "party_str", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = t3, ggplot2::aes(x = alt_swps, 
                                             y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch %>% filter(alt_swps < 5.1360000))+
  ggplot2::geom_ribbon(data = t3, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                            y = estimate, 
                                                            ymin = conf.low, 
                                                            ymax = conf.high)) +
  labs(title = "M = 2") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Party Strength on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"))  

drown1 / (drown3 | t3.1)

##data-vis-IV - M.E Ideological Distance----
zet00 <- plot_cme(model_h, variables = "dif1", condition = "alt_swps", type = "link", draw = F)
zet1 <- plot_cme(model_h, variables = "dif1", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = zet00, ggplot2::aes(x = alt_swps, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch) +
  ggplot2::geom_ribbon(data = zet00, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "Full Dataset") +
  xlab("Weighted Part. in Coal.") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black"))  

zef00 <- plot_cme(model_j, variables = "dif1", condition = "alt_swps", type = "link", draw = F)
zef1 <- plot_cme(model_j, variables = "dif1", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = zef00, ggplot2::aes(x = alt_swps, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch) +
  ggplot2::geom_ribbon(data = zef00, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "M = 1") +
  xlab("Weighted Part. in Coal.") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black"))  


t1 <- plot_cme(model_l, variables = "dif1", condition = "alt_swps", type = "link", draw = F)
t1.1 <- plot_cme(model_l, variables = "dif1", condition = "alt_swps", type = "link") +
  ggplot2::geom_line(data = t1, ggplot2::aes(x = alt_swps, 
                                             y = estimate), size = 0.8) +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch %>% filter(alt_swps < 5.1360000)) +
  ggplot2::geom_ribbon(data = t1, alpha = 0.3, ggplot2::aes(x = alt_swps, 
                                                            y = estimate, 
                                                            ymin = conf.low, 
                                                            ymax = conf.high)) +
  labs(title = "M = 2") +
  xlab("Weighted Part. in Coal.") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black"))  

zet1 / (zef1 | t1.1) -> Figure4.1


zet01 <- plot_cme(model_h, variables = "dif1", condition = "party_str", type = "link", draw = F)
zet2 <- plot_cme(model_h, variables = "dif1", condition = "party_str", type = "link") +
  ggplot2::geom_line(data = zet01, ggplot2::aes(x = party_str, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = party_str), data = BrSenSwitch %>% filter(party_str <= 0.51471278)) +
  ggplot2::geom_ribbon(data = zet01, alpha = 0.3, ggplot2::aes(x = party_str, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "Full Dataset") +
  xlab("Party Strength") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black"))  

zef01 <- plot_cme(model_j, variables = "dif1", condition = "party_str", type = "link", draw = F)
zef2 <- plot_cme(model_j, variables = "dif1", condition = "party_str", type = "link") +
  ggplot2::geom_line(data = zef01, ggplot2::aes(x = party_str, 
                                                y = estimate), size = 0.8) +
  geom_rug(aes(x = party_str), data = BrSenSwitch %>% filter(party_str <= 0.51471278)) +
  ggplot2::geom_ribbon(data = zef01, alpha = 0.3, ggplot2::aes(x = party_str, 
                                                               y = estimate, 
                                                               ymin = conf.low, 
                                                               ymax = conf.high)) +
  labs(title = "M = 1") +
  xlab("Party Strength") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black")) 

t2 <- plot_cme(model_l, variables = "dif1", condition = "party_str", type = "link", draw = F)
t2.1 <- plot_cme(model_l, variables = "dif1", condition = "party_str", type = "link") +
  ggplot2::geom_line(data = t2, ggplot2::aes(x = party_str, 
                                             y = estimate), size = 0.8) +
  geom_rug(aes(x = party_str), data = BrSenSwitch %>% filter(party_str <= 0.470512411)) +
  ggplot2::geom_ribbon(data = t2, alpha = 0.3, ggplot2::aes(x = party_str, 
                                                            y = estimate, 
                                                            ymin = conf.low, 
                                                            ymax = conf.high)) +
  labs(title = "M = 2") +
  xlab("Party Strength") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 40, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=40, family="regular", color="black"),
    axis.text.y = element_text(size=30, family="regular", color="black"),
    axis.text.x = element_text(size=30, family="regular", color="black"))  


zet2 / (zef2 | t2.1) -> Figure4.2

(Figure4.1 | Figure4.2)

##data-vis-V - Triple Interaction----
audio <- plot_cme(model_i, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link", draw = F)
audio1 <- plot_cme(model_i, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link") +
  ggplot2::geom_line(data = audio %>% filter(party_str == 0), ggplot2::aes(x = alt_swps, 
                                                                           y = estimate,
                                                                           color = factor(party_str)), size = 0.8) +
  scale_fill_discrete(labels = c("0%", "1.49%", "4.77%", "10.54%", "51.47%"), name = "Party \nStrength") +
  scale_colour_discrete(labels = c("0%", "1.49%", "4.77%", "10.54%", "51.47%"), name = "Party \nStrength") +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch) +
  #ggplot2::geom_ribbon(data = b1 %>% filter(party_str == 0.0504941655872341), alpha = 0.8, ggplot2::aes(x = alt_swps, 
  #                                                          y = estimate, 
  #                                                          ymin = conf.low, 
  #                                                          ymax = conf.high,
  #                                                          fill = party_str))
  labs(title = "Full Dataset") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"),
    ##Legend
    legend.position = "top",
    legend.background = element_rect(fill = "white"),
    legend.key = element_rect(fill = "white"),
    legend.title = element_text(margin = margin(b = 10), family="regular",size=15),
    text = element_text(size = 15))  

sil <- plot_cme(model_k, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link", draw = F)
sil1 <- plot_cme(model_k, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link") +
  ggplot2::geom_line(data = sil %>% filter(party_str == 0), ggplot2::aes(x = alt_swps, 
                                                                         y = estimate,
                                                                         color = factor(party_str)), size = 0.8) +
  scale_fill_discrete(labels = c("0%", "1.62%", "4.54%", "9.89%", "51.47%"), name = "Party \nStrength") +
  scale_colour_discrete(labels = c("0%", "1.62%", "4.54%", "9.89%", "51.47%"), name = "Party \nStrength") +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch) +
  #ggplot2::geom_ribbon(data = b1 %>% filter(party_str == 0.0504941655872341), alpha = 0.8, ggplot2::aes(x = alt_swps, 
  #                                                          y = estimate, 
  #                                                          ymin = conf.low, 
  #                                                          ymax = conf.high,
  #                                                          fill = party_str))
  labs(title = "M = 1") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"),
    ##Legend
    legend.position = "top",
    legend.background = element_rect(fill = "white"),
    legend.key = element_rect(fill = "white"),
    legend.title = element_text(margin = margin(b = 10), family="regular",size=15),
    text = element_text(size = 15))  

arg <- plot_cme(model_m, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link", draw = F)
arg1 <- plot_cme(model_m, variables = "dif1", condition = c("alt_swps", "party_str"), type = "link") +
  ggplot2::geom_line(data = arg %>% filter(party_str == 0), ggplot2::aes(x = alt_swps, 
                                                                         y = estimate,
                                                                         color = factor(party_str)), size = 0.8) +
  scale_fill_discrete(labels = c("0%", "1.37%", "5.05%", "11.48%", "47.05%"), name = "Party \nStrength") +
  scale_colour_discrete(labels = c("0%", "1.37%", "5.05%", "11.48%", "47.05%"), name = "Party \nStrength") +
  geom_rug(aes(x = alt_swps), data = BrSenSwitch %>% filter(alt_swps <= 5.036000)) +
  #ggplot2::geom_ribbon(data = b1 %>% filter(party_str == 0.0504941655872341), alpha = 0.8, ggplot2::aes(x = alt_swps, 
  #                                                          y = estimate, 
  #                                                          ymin = conf.low, 
  #                                                          ymax = conf.high,
  #                                                          fill = party_str)) 
  labs(title = "M = 2") +
  xlab("Weighted Participation in Coalition") + ylab("M. E. of Ideological Distance on P. S.") +
  theme(
    plot.title = element_text(family = "bold", hjust = 0.5, vjust = 0.5, size = 20, color = "black"),
    ##Background
    panel.border=element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "#ffffff", linetype = 'blank'),
    panel.background = element_rect(fill = "#ffffff"),
    ##Axes
    axis.title = element_text(size=20, family="regular", color="black"),
    axis.text.y = element_text(size=15, family="regular", color="black"),
    axis.text.x = element_text(size=15, family="regular", color="black"),
    ##Legend
    legend.position = "top",
    legend.background = element_rect(fill = "white"),
    legend.key = element_rect(fill = "white"),
    legend.title = element_text(margin = margin(b = 10), family="regular",size=15),
    text = element_text(size = 15))  

audio1 / (sil1 | arg1)